import axios, { ApiFormat } from '.';

export type TTarget = 'register' | 'login' | 'bind_phone' | 'change_phone' | 'verify_phone' | 'reset_password';

export interface ICreator {
	avatar: string;
	name: string;
	uid: number;
}

export interface IWorkItem {
	id: number; //
	name: string;
	cover: string;
	description: string;
	favorited: boolean; // 	是否收藏
	liked: boolean; // 	是否点赞
	num_favorites: number; //  	收藏数
	num_likes: number; //  	点赞数
	num_views: number; //  	浏览数
	creator: ICreator;
	tag_ids: number; //  	标签id
	updated_ts: number;
}

export interface IWorkDetail {
	id: number;
	name: string;
	cover: string;
	description: string;
	instruction: string;
	created_ts: number; //	 创建时间按戳，单位s
	status: number;
	favorited: boolean; //		是否收藏
	num_favorites: number; //		收藏数
	liked: boolean; //		是否点赞
	num_likes: number; //	 点赞数
	url: string;
	creator: ICreator; //		作者信息
	tag_ids: number[]; //		标签id
}

export const InitWorkDetail: IWorkDetail = {
	id: 0,
	name: '',
	cover: '',
	description: '',
	instruction: '',
	created_ts: 0, //	  创建时间按戳，单位s
	status: 0,
	favorited: false, //		是否收藏
	num_favorites: 0, //		收藏数
	liked: false, //		是否点赞
	num_likes: 0, //	  点赞数
	url: '',
	creator: {
		avatar: '',
		name: '',
		uid: 0,
	}, //		作者信息
	tag_ids: [], //		 标签id
};

export interface IUserDetail {
	uid: number;
	name: string;
	avatar: string;
	district: number; //	地区
	wechat_id: string; //  微信号
	signature: string; //  个性签名
	area_code: string; //  	常用手机区号
	contact: string; //  	常用手机号
	true_name: string; //  	真名
	gender: string; //  	性别 枚举类型[male:男 female:女]
	birthday: string; //	生日 到期时间 格式2010-01-01
	school: string;
	phone: string;
}

export const InitUserDetail: IUserDetail = {
	uid: 0,
	name: '',
	avatar: '',
	district: 0, //	地区
	wechat_id: '', //  微信号
	signature: '', //  个性签名
	area_code: '', //  	常用手机区号
	contact: '', //  	常用手机号
	true_name: '', //  	真名
	gender: '', //  	性别 枚举类型[male:男 female:女]
	birthday: '', //	生日 到期时间 格式2010-01-01
	school: '',
	phone: '',
};

export interface IArea {
	districts: Array<{
		code: number;
		name: string;
		cities: Array<{
			code: number;
			name: string;
			areas: Array<{
				code: number;
				name: string;
			}>;
		}>;
	}>;
}

export interface IClassroom2Group {
	courses: IClassroom2Course[];
	id: number;
	name: string;
}

export interface IClassroom2Course {
	auth: number;
	cover: string;
	id: number;
	lecturer_name: string;
	price: number;
	status: number;
	subject: number;
	tags: string[];
	title: string;
	first_lesson_id: number;
}

export const getVCodeByPhone = (
	phone: string,
	target: TTarget,
): Promise<{
	msg: string;
	error: string;
}> => {
	return axios.post('/workshop/api/v1/vcode', { phone, target });
};

export const registerUser = (params: {
	phone: string; //	手机号
	password: string; //	密码
	vcode: string; //	手机验证码
}): Promise<any> => {
	return axios.post('/workshop/api/v1/register', params);
};

export const loginUser = (params: {
	phone: string; //	手机号
	password: string; //	密码
}): Promise<any> => {
	return axios.post('/workshop/api/v1/login', params);
};

export const getUserWorks = (params: {
	offset: number;
	limit: number;
	sort?: 'newest' | 'hottest';
	tag?: number;
}): Promise<{
	items: IWorkItem[];
}> => {
	return axios.get('/workshop/api/v1/scratch_works', { params });
};

export const getClassroom2List = (params: {
	offset: number;
	limit: number;
}): Promise<{
	items: IClassroom2Group[];
}> => {
	return axios.get('/workshop/api/v1/course/groups', { params });
};

export const getUserWorksById = (params: {
	offset: number;
	limit: number;
	uid?: string | number;
}): Promise<{
	items: IWorkItem[];
}> => {
	return axios.get('/workshop/api/v1/user/scratch_works', { params });
};

export const getUserWorksLike = (params: {
	offset: number;
	limit: number;
	uid?: string | number;
}): Promise<{
	items: IWorkItem[];
}> => {
	return axios.get('/workshop/api/v1/user/scratch_works/liked', { params });
};

export const getUserWorksFavorite = (params: {
	offset: number;
	limit: number;
	uid?: string | number;
}): Promise<{
	items: IWorkItem[];
}> => {
	return axios.get('/workshop/api/v1/user/scratch_works/favorite', { params });
};

export const getUserWorksDetail = (
	id: string | number,
): Promise<{
	data: IWorkDetail;
}> => {
	return axios.get('/workshop/api/v1/scratch_work', { params: { id } });
};

export const resetPasswordByPhone = (params: {
	phone: string; //	手机号
	password: string; //	密码
	vcode: string; //	手机验证码
}): Promise<any> => {
	return axios.post('/workshop/api/v1/reset/password', params);
};

export const uploadFile = (
	file: any,
): Promise<{
	full_uri: string;
	uri: string;
}> => {
	const data = new FormData();
	data.append('file', file);
	return axios.post('/workshop/api/v1/upload', data, {
		headers: { 'Content-Type': 'multipart/form-data' },
	});
};

export const getArea = (): Promise<IArea> => {
	return axios.get('/workshop/api/v1/districts');
};

export const updateUserMsg = (data: IUserDetail): Promise<{}> => {
	return axios.put('/workshop/api/v1/mine', data);
};

export const getUserMsg = (): Promise<{ data: IUserDetail }> => {
	return axios.get('/workshop/api/v1/mine');
};

export const getUserBrief = (
	uid: string | number,
): Promise<{ data: { uid: number; name: string; signature: string } }> => {
	return axios.get('/workshop/api/v1/user/brief', { params: { uid } });
};

export const postProblem = (params: {
	title: string;
	content: string;
	email: string;
	images: string[];
}): Promise<{ data: IUserDetail }> => {
	return axios.post('/workshop/api/v1/feedback', params);
};

export const userLikeWork = (id: number | string): Promise<{ data: IUserDetail }> => {
	return axios.post('/workshop/api/v1/user/like', { id });
};

export const userCancelLikeWork = (id: number | string): Promise<{ data: IUserDetail }> => {
	return axios.delete('/workshop/api/v1/user/like', { data: { id } });
};

export const userFavoriteWork = (id: number | string): Promise<{ data: IUserDetail }> => {
	return axios.post('/workshop/api/v1/user/favorite', { id });
};

export const userCancelFavoriteWork = (id: number | string): Promise<{ data: IUserDetail }> => {
	return axios.delete('/workshop/api/v1/user/favorite', { data: { id } });
};

export const getSettings = (): Promise<{ data: { scratch_tags: Array<{ id: number; name: string }> } }> => {
	return axios.get('/workshop/api/v1/settings');
};

export const deleteWorkById = (id: number | string): Promise<{ data: IUserDetail }> => {
	return axios.delete('/workshop/api/v1/scratch_work', { data: { id } });
};
